package com.bkhc.business.api;

import com.alibaba.excel.EasyExcel;
import com.bkhc.business.entity.*;
import com.bkhc.business.entity.vo.ApplyInfomation;
import com.bkhc.business.entity.vo.Order;
import com.bkhc.business.entity.vo.OrderExcelItem;
import com.bkhc.business.entity.vo.OrderItem;
import com.bkhc.business.service.BusinessService;
import com.bkhc.common.annotation.*;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.OrderVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author pcq
 * @date 2021/6/20 17:57
 */
@WebRestController
@RequestMapping("manager")
@Api("业务管理服务")
@ApiPackage(code = "business-api", name = "业务管理服务", description = "后台业务管理维护操作接口")
public class BusinessController {
    @Autowired
    private BusinessService businessSerivce;

    @ApiOperation(code = "queryCustomerByGroup", name = "通过业务组查找所有客户", description = "通过业务组查找所有客户", type = "common")
    @AuditLog(object = "Customer", operation = "通过业务组查找所有客户", type = OperationEnum.query)
    @RequestMapping(path = "/group/customer", method = RequestMethod.POST)
    public ResultJson<?> queryCustomerByGroup(@RequestBody(required = false) Customer customer,
                                              @RequestParam(name = "pageIndex") int pageIndex,
                                              @RequestParam(name = "pageSize") int pageSize) {
        ResultJson<?> res = businessSerivce.queryCustomerByGroup(customer, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code = "updateCustomeridentity", name = "修改客户的级别", description = "修改客户的级别")
    @AuditLog(object = "Customer", operation = "修改客户的级别", type = OperationEnum.update)
    @RequestMapping(path = "/group/customer/identity", method = RequestMethod.POST)
    public ResultJson<?> updateCustomeridentity(@RequestBody(required = true) Customer customer) {
        ResultJson<?> res = businessSerivce.updateCustomeridentity(customer);
        return res;
    }

    @ApiOperation(code = "queryMemberByGroup", name = "通过业务组查找所有业务员", description = "通过业务组查找所有业务员", type = "common")
    @AuditLog(object = "Customer", operation = "通过业务组查找所有业务员", type = OperationEnum.query)
    @RequestMapping(path = "/group/member", method = RequestMethod.POST)
    public ResultJson<?> queryMemberByGroup(@RequestParam(name = "pageIndex") int pageIndex,
                                            @RequestParam(name = "pageSize") int pageSize) {
        ResultJson<?> res = businessSerivce.queryMemberByGroup(pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code = "queryMemberCustomer", name = "查找当前业务员的客户", description = "查找当前业务员的客户")
    @AuditLog(object = "Customer", operation = "查找当前业务员的客户", type = OperationEnum.query)
    @RequestMapping(path = "/group/member/customer", method = RequestMethod.POST)
    public ResultJson<?> queryMemberCustomer(@RequestParam(name = "memberId") String memberId,
                                             @RequestParam(name = "pageIndex") int pageIndex,
                                             @RequestParam(name = "pageSize") int pageSize) {
        ResultJson<?> res = businessSerivce.queryMemberCustomer(memberId, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code = "assignment", name = "给业务员分配客户", description = "给业务员分配客户")
    @AuditLog(object = "Customer", operation = "给业务员分配客户", type = OperationEnum.update)
    @RequestMapping(path = "/group/assignment", method = RequestMethod.POST)
    public ResultJson<?> assignmentCustomer(@RequestBody(required = true) Customer customer) {
        ResultJson<?> res = businessSerivce.assignmentCustomer(customer);
        return res;
    }

    @ApiOperation(code = "addMemberByManager", name = "添加业务员", description = "添加业务员", type = "common")
    @AuditLog(object = "Member", operation = "添加业务员", type = OperationEnum.update)
    @RequestMapping(path = "/group/addMember", method = RequestMethod.POST)
    public ResultJson<?> addMemberByManager(@RequestBody Member member) {
        ResultJson<?> res = businessSerivce.addMemberByManager(member);
        return res;
    }

    @ApiOperation(code = "generateBusinessAcount", name = "生成业务员账号", description = "生成业务员账号", type = "common")
    @AuditLog(object = "Member", operation = "添加业务员", type = OperationEnum.update)
    @RequestMapping(path = "/group/generateBusinessAcount", method = RequestMethod.POST)
    public ResultJson<?> generateBusinessAcount() {
        ResultJson<?> res = businessSerivce.generateBusinessAcount();
        return res;
    }

    @ApiOperation(code = "addGroupByAdmin", name = "添加业务合伙人", description = "添加业务合伙人")
    @AuditLog(object = "Group", operation = "添加业务合伙人", type = OperationEnum.update)
    @RequestMapping(path = "/group/add", method = RequestMethod.POST)
    public ResultJson<?> addGroupByAdmin(@RequestBody(required = true) Group group) {
        ResultJson<?> res = businessSerivce.addGroupByAdmin(group);
        return res;
    }

    /**
     * 提交客戶資料，申請用戶等級
     */
    @ApiOperation(code = "applyCustomerLevel", name = "申請客戶等級", description = "申請客戶等級", type = "common")
    @AuditLog(object = "Group", operation = "申請客戶等級", type = OperationEnum.add)
    @RequestMapping(path = "/group/applyCustomer", method = RequestMethod.POST)
    public ResultJson<?> applyCustomer(@RequestBody(required = true) Customer customer) {
        ResultJson<?> res = businessSerivce.applyCustomerLevel(customer);
        return res;
    }

    /**
     * 審批用戶等級申請
     */
    @ApiOperation(code = "approveApply", name = "工作流審批", description = "工作流審批", type = "common")
    @AuditLog(object = "Group", operation = "工作流審批", type = OperationEnum.update)
    @RequestMapping(path = "/group/approveApply", method = RequestMethod.POST)
    public ResultJson<?> approveApply(@RequestBody(required = true) CustomLevelWorkFlow workFlow) {
        ResultJson<?> res = businessSerivce.approveApply(workFlow);
        return res;
    }

    /**
     * 查看客户等级待审批记录 （ 0 同意 1 待审批 2 不同意）
     **/
    @ApiOperation(code = "queryWorkFlow", name = "审批记录查询", description = "工作流查询", type = "common")
    @AuditLog(object = "Group", operation = "工作流查询", type = OperationEnum.query)
    @RequestMapping(path = "/group/queryCustomIdentifyApply/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public ResultJson<?> queryCustomIdentifyApply(@PathVariable(name = "pageNum") int pageIndex, @PathVariable(name = "pageSize") int pageSize, @RequestBody(required = false) CustomLevelWorkFlow workFlow) {
        ResultJson<?> res = businessSerivce.queryCustomIdentifyApply(workFlow, pageIndex, pageSize);
        return res;
    }

    /**
     * 审批客户等级
     *
     * @param workFlow
     * @return
     */
    @ApiOperation(code = "operateWorkFlow", name = "操作工作流", description = "操作工作流", type = "common")
    @AuditLog(object = "Group", operation = "工作流操作", type = OperationEnum.query)
    @RequestMapping(path = "/group/operateCustomIdentifyApply", method = RequestMethod.POST)
    public ResultJson<?> operateCustomIdentifyApply(@RequestBody(required = true) CustomLevelWorkFlow workFlow) {
        ResultJson<?> res = businessSerivce.operateCustomIdentifyApply(workFlow);
        return res;
    }

    /**
     * 审批业务合伙人
     *
     * @param record
     * @return
     */
    @ApiOperation(code = "operateWorkFlow1", name = "操作工作流", description = "操作工作流", type = "common")
    @AuditLog(object = "Group", operation = "工作流操作", type = OperationEnum.query)
    @RequestMapping(path = "/group/operateBusinessApplyRecord", method = RequestMethod.POST)
    public ResultJson<?> operateBusinessApplyRecord(@RequestBody(required = true) ApplyRecord record) {
        ResultJson<?> res = businessSerivce.operateBusinessApplyRecord(record);
        return res;
    }

    /**
     * 查看业务合伙人待审批记录 （ 0 同意 1 待审批 2 不同意）
     **/
    @ApiOperation(code = "queryBusinessApplyRecord", name = "业务合伙人待审批记录", description = "工作流查询", type = "common")
    @AuditLog(object = "Group", operation = "工作流查询", type = OperationEnum.query)
    @RequestMapping(path = "/group/queryBusinessApplyRecord/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public ResultJson<?> queryBusinessApplyRecord(@PathVariable(name = "pageNum") int pageIndex, @PathVariable(name = "pageSize") int pageSize, @RequestBody(required = false) ApplyRecord record) {
        ResultJson<?> res = businessSerivce.queryBusinessApplyRecord(record, pageIndex, pageSize);
        return res;
    }

    /**
     * 修改业务员可查看价格的产品以及价格类型
     */
    @ApiOperation(code = "updateProductCode", name = "修改业务员可查看价格的产品以及价格类型", description = "修改业务员可查看价格的产品以及价格类型")
    @AuditLog(object = "Group", operation = "价格查看权限修改", type = OperationEnum.update)
    @RequestMapping(path = "/group/updatePriceAbout", method = RequestMethod.POST)
    public ResultJson<?> queryApply(List<BusinessPriceAccount> accounts) {
        ResultJson<?> res = businessSerivce.updatePriceAbout(accounts);
        return res;
    }

    /**
     * 查看邀请码
     *
     * @param
     * @return
     */
    @ApiOperation(code = "queryInviteCode", name = "查看邀请码", description = "查看邀请码", type = "common")
    @AuditLog(object = "Group", operation = "查看邀请码", type = OperationEnum.query)
    @RequestMapping(path = "/group/queryInviteCode", method = RequestMethod.POST)
    public ResultJson<?> queryInviteCode() {
        ResultJson<?> res = businessSerivce.queryInviteCode();
        return res;
    }

    /**
     * 修改订单审核状态
     *
     * @param
     * @return
     */
    @ApiOperation(code = "updateOrderAuditStatus", name = "修改订单审核状态", description = "修改订单审核状态", type = "common")
    @AuditLog(object = "Group", operation = "修改订单审核状态", type = OperationEnum.update)
    @RequestMapping(path = "/group/updateOrderAuditStatus", method = RequestMethod.POST)
    public ResultJson<?> updateOrderAuditStatus(@RequestBody(required = true) Order order) {
        ResultJson<?> res = businessSerivce.updateOrderAuditStatus(order);
        return res;
    }

    /**
     * 查询当前登录用户是否为企业用户
     *
     * @param
     * @return
     */
    @ApiOperation(code = "findMember", name = "查询当前登录用户是否为企业用户", description = "查询当前登录用户是否为企业用户", type = "common")
    @AuditLog(object = "Group", operation = "查询当前登录用户是否为企业用户", type = OperationEnum.query)
    @RequestMapping(path = "/group/queryMemberIsPerson", method = RequestMethod.POST)
    public ResultJson<?> queryMemberIsPerson() {
        ResultJson<?> res = businessSerivce.queryMemberIsPerson();
        return res;
    }

    @ApiOperation(code = "exportItem", name = "导出报价单详情", description = "导出报价单详情", type = "common")
    @AuditLog(object = "Group", operation = "导出报价单详情", type = OperationEnum.query)
    @RequestMapping(path = "/group/exportItem", method = RequestMethod.POST)
    public void exportTable(HttpServletResponse response, OrderVo orderVo) throws IOException {
        //所有数据
        List<OrderExcelItem> orderExcelItems = businessSerivce.getData(orderVo);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String exportName = URLEncoder.encode("报价单详情", "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" +
                exportName + ".xlsx");


        EasyExcel.write(response.getOutputStream(), OrderExcelItem.class).sheet().doWrite(orderExcelItems);

    }
}
